﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebPagesStarterKit;
using System.Xml;

public partial class UserControls_SuperFooter : System.Web.UI.UserControl
{
    string template = "<td valign=\"top\">\n\t\t\t<div class=\"footerlinkstitle\">{0}</div>\n";
    string link = "\t\t\t<div class=\"footerlink\"><a  href=\"{0}\">{1}</a></div>\n";
    string end = "\n\t\t\t</td>\n\t\t\t";
    int menuCount = 4;

    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMapNode rootNode = SiteMap.Provider.FindSiteMapNode("~/superFooter.aspx");
        if (rootNode != null)
        {
            litLinks.Text = string.Empty;

            foreach (SiteMapNode node in rootNode.ChildNodes)
            {
                litLinks.Text += string.Format(template, node.Title);
                CreateSubNodes(node);
            }
        }
    }

    private void CreateSubNodes(SiteMapNode child)
    {
        int i = 1;
        if(child.HasChildNodes)
            foreach (SiteMapNode node in child.ChildNodes)
            {
                if (!string.IsNullOrEmpty(node["linkedto"]))
                {
                    WebPage page = new WebPage(Convert.ToString(node["linkedto"]));
                    litLinks.Text += string.Format(link, page.VirtualPath.Replace("~",""), node.Title);
                }
                else
                {
                    litLinks.Text += string.Format(link, node.Url, node.Title);
                }
                if (i == menuCount)
                    break;
                i++;
            }
        litLinks.Text += end;
    }
}